package ejb.bl;

import hibernate.dao.Customer;

import java.util.List;

import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 * Session Bean implementation class MusicStoreBL
 */
@Stateless(name = "MusicStoreStalessBean")
@Remote(value = MusicStoreBLRemote.class)
public class MusicStoreBL implements MusicStoreBLRemote {
	@PersistenceContext(unitName = "PersistenceUnit")
	private EntityManager entityManager;

	/**
	 * Default constructor.
	 */
	public MusicStoreBL() {
		// TODO Auto-generated constructor stub
	}

	public String getCustomerNameById(int id) {
		// TODO Auto-generated method stub
		String cusNam;
		try {
			cusNam = entityManager.find(Customer.class, id).getName();
		} catch (RuntimeException e) {
			// TODO: handle exception
			throw e;
		}
		return cusNam;
	}

	public boolean checkClientLogin(int userId, String password) {
		// TODO Auto-generated method stub
		boolean result = false;
		try {
			if (password.equals(entityManager.find(Customer.class, userId)
					.getPassword())) {
				result = true;
			} else {
				result = false;
			}
		} catch (RuntimeException e) {
			throw e;
		}

		return result;
	}

	public List loadCdList() {
		try {
			Query query = entityManager
					.createQuery("SELECT idCd, image, title, genre FROM MusicCd");
			return query.getResultList();
		} catch (RuntimeException e) {
			// TODO: handle exception
			throw e;
		}
	}

	public List loadSongByCdId(String cdId) {
		// TODO Auto-generated method stub
		try {
			Query query = entityManager
					.createQuery("SELECT a.song FROM CdSong a WHERE a.musicCd.idCd = "+ cdId);
			return query.getResultList();
		} catch (RuntimeException e) {
			// TODO: handle exception
			throw e;
		}
	}
}
